package com.dongdongshop.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Autowired DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        /**
         * shiro常用的过滤器
         * anon:表示无需认证即可放过,
         * authc:表示必须认证才能访问
         * perms:表示当前的方法必须由某个权限才能访问
         * logout:退出登录过滤器
         */
        //对比springMVC的拦截器我们添加一下shiro的过滤器
        Map<String, String> map = new LinkedHashMap<>();//使用linkedHashMap就是为了顺序
//        map.put("/tbUser/**","anon");
////        map.put("/tbUser/login","anon");
////        map.put("/tbUser/register","anon");
////        map.put("/tbUser/getPhone","anon");
//        map.put("/content/getShuff","anon");
//        map.put("/content/getLikeList","anon");
//        map.put("/content/getDayList","anon");
//        map.put("/","anon");
//        map.put("/js/**", "anon");
//        map.put("/css/**","anon");
        map.put("/my/**","anon");
//        map.put("/plugins/**","anon");
//        map.put("/logout","logout");//随便输一个路劲拦截器自动拦截然后看是否有没有后你定义的路径如果有就拦截,这个路劲就是一个名字不需要进行跳转

//        map.put("/test/add", "perms[add]");
//        map.put("/test/delete", "perms[delete]");//状态码 401 表示无权访问

        map.put("/my/**", "authc");

        //登录拦截后跳转的路径
        shiroFilterFactoryBean.setLoginUrl("/tbUser/login");

        //当我访问没有权限的方法,shiro就会默认的跳转错误页面,我们可以通过以下的配置自定义跳转页面
//        shiroFilterFactoryBean.setUnauthorizedUrl("/user/Forbidden");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        return shiroFilterFactoryBean;
    }

    //创建安全管理器对象
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Autowired LoginRealm loginRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //安全管理器需要管理登录和授权这些功能
        securityManager.setRealm(loginRealm);//直接使用@Autowird进行注入也可以
        return securityManager;
    }

    //此步骤就是创建我们自定义的LoginRealm,把自定义的内容的对象配置进来给setRealm进行传值;
    //配置注入LoginRealm中的LoginRealm对象,也可以用注解的方式进行注入
    @Bean
    public LoginRealm loginRealm(@Autowired HashedCredentialsMatcher hashedCredentialsMatcher) {
        LoginRealm loginRealm = new LoginRealm();
        loginRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return loginRealm;
    }
//    开启密码的加密
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        //设置加密方式 sha-128 MD5 MD4 SHA-256 ....
        matcher.setHashAlgorithmName("MD5");
//        设置加密的次数
        matcher.setHashIterations(10);
//        设置编码
        matcher.setStoredCredentialsHexEncoded(true);
        return matcher;
    }
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
}
